define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/meal/index' + location.search,
                    add_url: 'user/meal/add',
                    edit_url: 'user/meal/edit',
                    del_url: 'user/meal/del',
                    multi_url: 'user/meal/multi',
                    import_url: 'user/meal/import',
                    table: 'user_meal',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"day":__('Day'),"month":__('Month'),'year':__('Year')},formatter: function (v) {
                            if(v == 'day')   {
                                return __('Day');
                            }

                            if(v == 'month'){
                                return __('Month');
                            }
                            if(v == 'year'){
                                    return __('Year');
                                }
                            }},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'icon', title: __('Icon'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'original_price', title: __('Original_price'), operate:'BETWEEN'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'discount', title: __('Discount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'sales', title: __('Sales')},
                        {field: 'days', title: __('Days')},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'addsub',
                                    text: '会员权益',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    icon: 'fa fa-plus',
                                    url: 'user/meal_rule/index/meal_id/{ids}'
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
